package com.bridgetree.jubaocrm.ui;

import java.util.HashMap;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.bridgetree.jubaocrm.R;
import com.bridgetree.jubaocrm.model.CRMUserBaseInfo;
import com.bridgetree.jubaocrm.service.Network;
import com.bridgetree.jubaocrm.ui.project.XMChildActivity;
import com.bridgetree.jubaocrm.util.Announcement;
import com.bridgetree.jubaocrm.util.LoginError;

/**
 *	公司公告适配器
 */
public class PageCompanyAnnouncementAdapter extends BaseAdapter {
	private List<Announcement>  Date = null;
	private Context context;
	private LayoutInflater mInflater;
	public HashMap<Integer, Bitmap> cachedBitmap;

	private class GridHolder {
		TextView tv;
	}
	
	@SuppressLint("UseSparseArrays")
	public PageCompanyAnnouncementAdapter(Context mContext,List<Announcement> mAnnouncementList) {
		super();
		this.context = mContext;
		this.Date = mAnnouncementList;
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public int getCount() {
		return Date.size();
	}

	@Override
	public Object getItem(int index) {
		return Date.get(index);
	}

	@Override
	public long getItemId(int index) {
		return index;
	}

	@Override
	public View getView(final int index, View convertView, ViewGroup parent) {
		GridHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.xm_12_item, null);
			holder = new GridHolder();
			holder.tv = (TextView) convertView.findViewById(R.id.xm_12_item_tv);
			convertView.setTag(holder);
		} else {
			holder = (GridHolder) convertView.getTag();
		}
		
		final String xm_txt=Date.get(index).getTitle();
		holder.tv.setText(xm_txt);
		convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				getCompanyGongGao(Date.get(index).getId());
			}
		});
		return convertView;
	}
	
	private void openDailog2(String title, String content) {
		new AlertDialog.Builder(context).setTitle(title)
		.setMessage(content).setPositiveButton("确定",null).show();
	}

	/**
	 * 获取公司公告详情的数据
	 * 
	 * @param pageIndex
	 */
	private void getCompanyGongGao(String ggid) {
		Network mNetword = new Network(context);
		if (!mNetword.isNetworkConnected()) {
			openDailog2("网络提示", "网络未连接,请连接网络！");
		} else {
			SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
			String user_id = prefs.getString(Contents.KEY_PERSONAL_LOGIN_USEID, ""); 
			String token = prefs.getString(Contents.KEY_PERSONAL_LOGIN_TAKEN, ""); 
			
			String URL = Network.API_DOMAIN + "/oneinfo?id=" + ggid
			+ "&user_id=" + user_id
			+ "&token=" + token + "&validation="
			+ CRMUserBaseInfo.kValication;
//			公司公告详情：http://crmsoap.woniuwo.cn/oneinfo?id=
			//536c365fd5ac70725f586433
			//&user_id=3a2ca74f8b484303ade9b7e2655994d9
			//&token=73d06ddd67044304b6b96419cfbcf5ca
			//&validation=gAJ9cQAoVQZsb2NhbGVxAVUFemhfQ05xAl
			
			final ProgressDialog progressDialog = ProgressDialog.show(context,
					"网络数据", "...Loading...");
			RequestQueue requestQueue = Volley.newRequestQueue(context);
			JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
					Request.Method.GET, URL, null,
					new Response.Listener<JSONObject>() {
						@Override
						public void onResponse(JSONObject response) {
							if (progressDialog.isShowing()
									&& progressDialog != null) {
								progressDialog.dismiss();
							}
							System.out.println("公司公告详情 response=" + response);
							try {
								if (!response.isNull("info")) {
									JSONObject jsonObj2 = response.getJSONObject("info");
									Announcement mAnnouncement = new Announcement();
									if (!jsonObj2.isNull("fid")) {
										String id = jsonObj2
										.getString("fid");
										mAnnouncement.setId(id);
									}
									if (!jsonObj2.isNull("content")) {
										String content = jsonObj2
										.getString("content");
										mAnnouncement.setContent(content);
									}
									if (!jsonObj2.isNull("title")) {
										String title = jsonObj2
										.getString("title");
										mAnnouncement.setTitle(title);
									}
									if (!jsonObj2.isNull("expire_date")) {
										String expire_date = jsonObj2
										.getString("expire_date");
										mAnnouncement.setExpire_date(expire_date);
									}
									Intent intent = new Intent(context,CompanyAnnouncementActivity.class);
									Bundle bundle = new Bundle();
									bundle.putSerializable("mAnnouncement", mAnnouncement);
									intent.putExtras(bundle);
									((Activity) context).startActivity(intent);
									
								}
								if (!response.isNull("errors")) {
									String errors = response.get("errors")
									.toString();
									if (errors.equals("[]")) {
									} else {
										org.json.JSONArray jsonArr;
										if (!response.isNull("errors")) {
											LoginError mLoginError = new LoginError();
											jsonArr = response
											.getJSONArray("errors");
											if (jsonArr.length() > 0) {
												for (int i = 0; i < jsonArr
												.length(); i++) {
													JSONObject jsonObj2 = jsonArr
													.getJSONObject(i);
													if (!jsonObj2
															.isNull("desc")) {
														String desc = jsonObj2
														.getString("desc");
														mLoginError
														.setDesc(desc);
													}
													if (!jsonObj2
															.isNull("value")) {
														String value = jsonObj2
														.getString("value");
														mLoginError
														.setValue(value);
													}
												}
											}
										}
									}
								}
							} catch (NumberFormatException e) {
								e.printStackTrace();
							} catch (JSONException e) {
								e.printStackTrace();
							}
						}
					}, new Response.ErrorListener() {
						@Override
						public void onErrorResponse(VolleyError arg0) {
							System.out.println("sorry,Error");
						}
					});
			requestQueue.add(jsonObjectRequest);
		}
	}
}

